<?php
/**
 * @copyright meila.com
 * @author shaopu@
 * @name 分析商品标签
 * @param 
 *         $title = 商品标题
 * @static 
 *         XXX = 作用
 * 
 * 
 */




class ml_biz_analyseTags
{
    //商品原标题
    private $title = '';
    //分词结果原数据
    private $aSegResult = array();
    //分析完的原始标签
    private $aOriTags = array();
    //手动添加的标签
    private $handleTags = array();
    //对比后台标签库得到可用的标签
    private $aAvailTags = array();
    
    //分词接口
    const SEGMENT_URL = 'http://1.shopfenci.sinaapp.com/segment.php?key=';
    
    //分词接口返回的词性
    const WORDTYPE_NOUN = 95;//名词
    

    /**
     * 开始分析
     *
     * @param string $title 商品标题
     * return void
     */
    public function start($title)
    {
        $this->_reset();
        $this->title = ml_tool_tagfilter::filter($title);
        return ;
    }
    /**
     * 分词
     *
     * @return bool
     */
    public function segment()
    {
        //进行分词
        $aWord = $this->_segment($this->title);
        
        

        //过滤无用标签
        foreach ($aWord as $row)
        {
            $this->aSegResult[] = $row;
            $aTag[] = $row;
            if($this->_is_format_ok($row))
                $this->aOriTags[] = $row;
        }
        
        
        return true;
    }
    /**
     * 手动添加标签
     * @wangtao5
     *
     * @return bool
     */
    public function add_tag($tag_array){
        if(!is_array($tag_array)) return false;
        $this->handleTags = $tag_array;
        return true;
    }

    /**
     * 与后台标签库对比 标签是否存在
     *
     * @return bool
     */
    public function check_tag_exists()
    {
        $alltag_n = count($this->aOriTags);
        
        //看看哪些是现有标签
        $oAdmTag = new ml_model_admin_dbCommon();
        $aTag = $this->aOriTags;
        if(count($this->handleTags)>0){
            if($alltag_n>0){
                $aTag = array_merge($aTag, $this->handleTags);
            }else{
                $aTag = $this->handleTags;
            }
            $aTag = array_unique($aTag);
        }
        if(!$oAdmTag->tags_get_by_tag($aTag))
            return false;

        $this->aAvailTags = $oAdmTag->get_data();
        
        $tags = Tool_array::format_2d_array($this->aAvailTags , 'tag' , Tool_array::FORMAT_VALUE_ONLY);
        $aNotag = array_diff($this->aOriTags, $tags);
        Tool_logger::dataLog('segment' ,implode(' ' , $aNotag) , true , true , false);



        return true;
    }
    /**
     * 基于标签分析品类
     *
     * @return int 品类ID
     */
    public function analyse_catelog()
    {
        $ctg_id = $this->_check_last_tag();
        return $ctg_id;
    }

    public function analyse_color()
    {
        foreach ($this->aAvailTags as $row) {
            if($row['type'] == ML_TAGTYPE_COLOR)
            {
                return $row['sub_type'];
            }
        }
        return 0;
    }
    
    public function analyse_sex()
    {
        if(strpos($this->title, '男') !== false &&  strpos($this->title, '女') === false)
            return ML_SEX_BOY;
        else if(strpos($this->title, '女') !== false &&  strpos($this->title, '男') === false)
            return ML_SEX_GIRL;
        else
            return ML_SEX_ALL;
           
    }

    public function get_all_tags()
    {
        return $this->aOriTags;
    }
    
    public function get_available_tags()
    {
        return $this->aAvailTags;
    }
    
    
    /**
     * 分析品类方法
     * 倒序查找名词中包含的关键字 如：裤 衣
     *
     * @return int
     */
    private function _check_last_tag()
    {
        $aW2c = ml_factory::load_standard_conf('word2ctg');
        $aSegRs = array_reverse($this->aSegResult);
        foreach ($aSegRs as $row)
        {
                foreach ($aW2c as $word => $ctg_id)
                {
                    if(strpos($row , $word) !== false)
                        return $ctg_id;
                }
        }
        return 0;
    }
    
    private function _is_format_ok($tag)
    {
        if(strlen($tag) < 2)
            return false;
        
        else 
            return true;
    }
    
    public function _reset()
    {
        $this->title = '';
        $this->aOriTags = array();
        $this->aAvailTags = array();
        $this->aSegResult = array();
        $this->handleTags = array();
    }

    private function _segment($str)
    {
        $scws = scws_new();
        $scws->set_charset('utf8');
        $scws->set_dict('/www/wdlinux/scws/dict.utf8.xdb');
        $scws->set_rule('/www/wdlinux/scws/rules.utf8.ini');
        $scws->add_dict(SERVER_ROOT_PATH.'/include/config/scws/dict_meila.txt', SCWS_XDICT_TXT);
        $scws->set_multi($data['option'] & SCWS_TEST_MULTI_MASK);
        $scws->set_ignore(($data['option'] & SCWS_TEST_IGNORE_SYMBOL) ? true : false); 
        $scws->set_duality(($data['option'] & SCWS_TEST_DUALITY) ? true : false); 
        $scws->send_text($str);
        while ($result = $scws->get_result())
        {
            foreach ($result as $tmp)
                $words[] = $tmp['word'];
        }
        return $words;
    }

}
/*
$title = 'CC公主2012春装装新款女装甜美仙女珍珠蕾丝领长袖连衣裙针织裙';
$a = new ml_biz_analyseTags();
$a->start($title);
$re = $a->segment();
echo $re;
$re = $a->check_tag_exists();
echo $re;
$q = $a->analyse_catelog();
var_dump($q);
$w = $a->get_all_tags();
var_dump($w);
$e = $a->get_available_tags();
var_dump($e);
*/